package com.zjw;

import com.zjw.entity.*;
import com.zjw.service.GoodsService;
import com.zjw.service.TypeService;
import com.zjw.service.UserService;
import com.zjw.service.impl.GoodsServiceImpl;
import com.zjw.service.impl.TypeServiceImpl;
import com.zjw.service.impl.UserServiceImpl;
import com.zjw.utils.Constants;
import com.zjw.vo.GoodsDetail;

import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class UserMenu {
    private User user;

    public UserMenu(User user) {
        this.user = user;
    }

    private final GoodsService goodsService = new GoodsServiceImpl();
    private final Scanner input = new Scanner(System.in);

    public void showUserMenu() {
        userInfo();
        do {
            System.out.println("1.查看好物列表");
            System.out.println("2.上架新的好物");
            System.out.println("3.查看我的在售好物");
            System.out.println("4.账户操作");
            System.out.println("0.退出登录");
            System.out.println("请输入您要执行的操作：");
            int choice = input.nextInt();
            switch (choice) {
                case 1:
                    CommonOperate.getGoodsList(Constants.LOGIN_TYPE_USERNAME , user.getUserId());
                    break;
                case 2:
                    addGoods();
                    break;
                case 3:
                    showMySellingGoods();
                    break;
                case 4:
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入有误，请重新输入");
            }
            System.out.println();
        } while (true);
    }

    public void userInfo() {
        System.out.println("用户名：" + user.getUserName());
        System.out.println("账户余额：" + user.getBalance());
    }



    /**
     * 上架好物
     */
    public void addGoods(){
        TypeService typeService = new TypeServiceImpl();
        GoodsService goodsService = new GoodsServiceImpl();
        List<Type> typeList = typeService.findAll();
        System.out.print("请输入好物名称：");
        String goodsName = input.next().trim();
        System.out.print("\n请选择好物类别");
        System.out.print("(");
        for (Type type : typeList) {
            System.out.println(type.getTypeId() + "." + type.getTypeName());
        }
        System.out.print(")");
        int typeId = input.nextInt();
        for (int i = 0; i < typeList.size(); i++) {
            if (typeId == typeList.get(i).getTypeId()){
                break;
            }
            if (i == typeList.size() - 1) {
                System.out.println("输入错误，请重新输入");
                addGoods();
                break;
            }
        }
        System.out.print("\n请输入好物价格：");
        double money = input.nextInt();
        if (money < 0){
            System.out.println("输入要大于0");
            addGoods();
        }
        System.out.println("请输入好物介绍：");
        String introduce = input.next().trim();
        Goods goods = new Goods(goodsName , money , introduce , new Date() , Constants.GOODS_STATUS_ON_SALE , typeId , user.getUserId());
        if (goodsService.addGoods(goods)) {
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
    }


    /**
     * 查看我的在售好物
     */
    public void showMySellingGoods(){
        GoodsService goodsService = new GoodsServiceImpl();
        Page page = new Page();
        page.setTotalCount(goodsService.queryGoodsCountByUserId(user.getUserId()));
        do {
            System.out.println("编号\t\t名称\t\t类型\t\t上架时间\t\t描述");
            for (Goods goods : goodsService.queryGoodsByUserIdAndPaging(page.getCurrPageNo(), page.getPageSize(), user.getUserId())) {
                System.out.println(goods.getGoodsId()+ "\t\t" + goods.getGoodsName() + "\t\t" + goods.getTypeName() + "\t\t" + goods.getGoodsAddTime() + "\t\t" + goods.getGoodsIntroduce());
            }
            System.out.println("页数/总页数：" + page.getCurrPageNo() + "/" + page.getTotalPageCount());
            System.out.println("1.上一页 2.下一页 0.返回上一级");
            int choice = input.nextInt();
            switch (choice){
                case 1:
                    int currPageNo = page.getCurrPageNo();
                    page.setCurrPageNo(page.getCurrPageNo() - 1);
                    if (currPageNo == page.getCurrPageNo()){
                        System.out.println("已是首页，不能再向前翻页");
                    }
                    break;
                case 2:
                    if (page.getCurrPageNo() + 1 > page.getTotalPageCount()){
                        System.out.println("已是末页，不能再向后翻页");
                    }
                    page.setCurrPageNo(page.getCurrPageNo() + 1);
                    break;
                case 0:
                    page = null;
                    break;
                default:
                    System.out.println("输入有误！");
                    showMySellingGoods();
            }
        }while (page != null);
    }

}
